//
//  BaseNavigationController.m
//  FloatClouds
//
//  Created by 黄威 on 16/8/25.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "BaseNavigationController.h"

@implementation BaseNavigationController
-(instancetype)initWithRootViewController:(UIViewController *)rootViewController{
    self = [super initWithRootViewController:rootViewController];
    if(self){
        
        NSUserDefaults *nightIsOn = [NSUserDefaults standardUserDefaults];
        NSString *isOn = [nightIsOn objectForKey:@"night"];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(ON)
                                                     name:@"NIGHTON"
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(OFF)
                                                     name:@"NIGHTOFF"
                                                   object:nil];
           // self.navigationBar.translucent = NO;
        
            if ([isOn isEqualToString:@"0"]) {
            
            self.navigationBar.barTintColor = [UIColor colorWithRed:18.0 / 255 green:86.0 / 255 blue:138.0 / 255 alpha:1];
       
            
            
        }else if([isOn isEqualToString:@"1"]){
            
          self.navigationBar.barTintColor = [UIColor colorWithRed:93/255.0 green:92/255.0 blue:101/255.0 alpha:1];
    
        }
        
    }
    return self;
    
}



-(void)pushViewController:(UIViewController*)viewController animated:(BOOL)animated{
    
    [super pushViewController:viewController animated:animated];
    
    //NavigationController中包含的第一个UIViewController也是push方法进来的 但是一般这个controller不需要添加back button
    
    if(self.viewControllers.count>1) {
        
        UIButton*backButton=[UIButton buttonWithType:UIButtonTypeCustom];
        
        backButton.frame=CGRectMake(0,0,40,30);
        
        [backButton setTitle:@"返回" forState:UIControlStateNormal];
        
        [backButton addTarget:self action:@selector(back)forControlEvents:UIControlEventTouchUpInside];
        
        viewController.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:backButton];
        
    }
    
}
-(void)back{
    
    [self popViewControllerAnimated:YES];
    
}
    
- (void)ON{
    
  self.navigationBar.barTintColor =  [UIColor colorWithRed:93/255.0 green:92/255.0 blue:101/255.0 alpha:1];
    
    
}

- (void)OFF{
    //lanse
   self.navigationBar.barTintColor = [UIColor colorWithRed:18.0 / 255 green:86.0 / 255 blue:138.0 / 255 alpha:1];
    
    
}

- (void)dealloc{
    
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"NIGHTON" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"NIGHTOFF"object:nil];
    
    
}
@end
